package com.gack.business.dao;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gack.business.model.BusinessCardBind;
import com.gack.business.model.OrderBind;

@Service
public class OrderDao {
	@Autowired
	private EntityManager entityManager;

	/**
	 * 
	 * @Title: findAllOrder 
	 * @Description: 查询用户订单 0全部-1使用中-2已完成-3已取消-4预约中-5待支付 
	 * @param status 订单状态 1>>预约  2>>主动取消预约订单关闭  3>>预约超时订单关闭  4>>使用中  5>>主动发起结束订单 6>>订单预结算(已选择商务卡并扣除) 7 订单带已支付金额已确定选择支付渠道  8>>用户主动支付订单  9>>商务卡全额支付订单 10>>优惠卷全额支付
	 * @param type 微信app(wx)支付宝app(alipay),free(商务卡全额支付)
	 * @param time 0 全部 1今天 2本周 3本月 4本季度
	 * @param used 0未开票 1开票中 2已开票 
	 * @param page 页数 
	 * @param pageSize 页面展示数据数 
	 * @return List<OrderBind> 
	 * @throws
	 */
	public Map<String, Object> findAllOrder(String status, String type, String time, Integer used, int page, int pageSize) {

//		String jpql = "select new com.gack.business.model.OrderBind(o.id,o.orderId,(SELECT used from Invoice i where o.id = i.oid) as invoiceStatus,o.amount,o.businessCardAmount,o.type,o.status,(SELECT storeName from VideoStores s where s.id = o.storeId) as storeName) from Order o where 1 = 1";
		String jpql = "select new com.gack.business.model.OrderBind(o.id,o.orderId,o.amount,o.businessCardAmount,o.type,o.status,(SELECT storeName from VideoStores s where s.id = o.storeId) as storeName) from Order o where 1 = 1";
		String jpql1 = "select new com.gack.business.model.OrderBind(o.id,o.orderId,o.amount,o.businessCardAmount,o.type,o.status,(SELECT storeName from VideoStores s where s.id = o.storeId) as storeName) from Order o where 1 = 1";

		// String beginTime = new SimpleDateFormat("yyyy-MM-dd
		// HH:mm:ss").format(new Date(Long.parseLong(timeStamp)));
		// String beginTime = new SimpleDateFormat("yyyy-MM-dd
		// HH:mm:ss").format(new Date(System.currentTimeMillis()));

		// jpql += "where o.beginTime > '" + beginTime + "'";

		if (status != null && status.trim().length() != 0 && !status.equals("0")) {
//			if (status.equals("1")) {
//				status = "4";
//			} 
//			if (status.equals("2")) {
//				status += "8,9,10";
//			} 
//			if (status.equals("3")) {
//				status += "2,3";
//			} 
//			if (status.equals("4")) {
//				status += "8,9,10";
//			}
//			if (status.equals("5")) {
//				status += "8,9,10";
//			}
			switch (status) {
			case "1":
				status = "'4'";
				break;
			case "2":
				status = "'8','9','10'";
				break;
			case "3":
				status = "'2','3'";
				break;
			case "4":
				status = "'1'";
				break;
			case "5":
				status = "'5','6','7'";
				break;
			default:
				break;
			}
			jpql += " and o.status in (" + status + ")";
			jpql1 += " and o.status in (" + status + ")";
		}

//		if (type != null && type.trim().length() != 0)
//			jpql += " and o.type = :type";
//		if (used != null)
//			jpql += " and (SELECT used from Invoice i where o.id = i.oid) = :used";

		if (time != null && !time.equals("") && !time.equals("0")) {
			Date date = null;
			if(time.equals("1")) {
				date = day();
			}else if(time.equals("2")) {
				date = week();
			} else if(time.equals("3")) {
				date = month();
			} else if(time.equals("4")) {
				date = quarter();
			}
				
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			
			time = format.format(date);
			
			jpql += " and o.createdTime > '" + time + "'";
			jpql1 += " and o.createdTime > '" + time + "'";
		}
System.out.println(jpql);
		Query query = entityManager.createQuery(jpql, OrderBind.class);
		Query query1 = entityManager.createQuery(jpql1, OrderBind.class);
//		if (status != null && status.trim().length() != 0 && !status.equals("0"))
//			query.setParameter("status", status);
//		if (type != null && type.trim().length() != 0)
//			query.setParameter("type", type);
//		if (used != null)
//			query.setParameter("used", used);

		@SuppressWarnings("unchecked")
		List<OrderBind> lists = query.setFirstResult(page * pageSize).setMaxResults(pageSize).getResultList();
		Map<String, Object> map = new HashMap<>();
		map.put("orders", lists);
		map.put("size", query1.getResultList().size());
		return map;
	}

	/**
	 * 
	 * @Title: findAllBusinessCard 
	 * @Description: 商务卡发票后台管理 
	 * @param used 0未开票 1开票中 2已开票 
	 * @param time 
	 * @param page 
	 * @param pageSize 
	 * @return List<BusinessCardBind> 
	 * @throws
	 */
	public List<BusinessCardBind> findAllBusinessCard(String used, String time, int page, int pageSize) {
		String jpql = "select new com.gack.business.model.BusinessCardBind(b.id,b.oid,(select name from Commodity c where c.id = b.cid ) as commodityName,b.number,(select nickname from User u where u.id = b.uid) as userName,(select name from Enterprise e where e.id = b.eid) as enterpriseName,b.createTime,b.amount,(select used from Invoice i where i.oid = b.id) as invoiceStatus) from BusinessCardRecord b";

		Query query = entityManager.createQuery(jpql, BusinessCardBind.class);
		List<BusinessCardBind> s = query.setFirstResult(page * pageSize).setMaxResults(pageSize).getResultList();
		return s;

	}

	public static Date day() {
//		return System.currentTimeMillis() / (1000 * 3600 * 24) * (1000 * 3600 * 24)
//				- TimeZone.getDefault().getRawOffset();
		Calendar calendar = Calendar.getInstance();
		
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}
	
	public static Date week(){
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); 
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}
	
	public static Date month(){
		Calendar calendar = Calendar.getInstance();    
		calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}
	
	public static Date quarter(){
		Calendar calendar = Calendar.getInstance();    
		switch (calendar.get(Calendar.MONTH)) {
		case 0:
		case 1:
		case 2:
			calendar.set(Calendar.MONTH, 0);
			break;
		case 3:
		case 4:
		case 5:
			calendar.set(Calendar.MONTH, 3);
			break;
		case 6:
		case 7:
		case 8:
			calendar.set(Calendar.MONTH, 6);
			break;
		case 9:
		case 10:
		case 11:
			calendar.set(Calendar.MONTH, 9);
			break;
		}
//		第一季度：1月－3月 
//		第二季度：4月－6月 
//		第三季度：7月－9月
//		第四季度：10月－12月
		calendar.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}
	
}
